package com.cskaoyan.controller.back;

import com.cskaoyan.bean.po.BasePageInfo;
import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.vo.back.AdminCreateVO;
import com.cskaoyan.bean.vo.back.AdminListVO;
import com.cskaoyan.bean.vo.back.AdminUpdateVO;
import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.service.back.AdminManagerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 管理员管理
 *
 * @author 12-江涛
 * @since 2022/10/12  13:14
 */
@RestController
@RequestMapping("admin")
public class AdminManagerController {

    @Autowired
    AdminManagerService adminService;

    /*
     * 管理员列表
     * http://182.92.235.201:8083/admin/admin/list
     *
     * @Author 12-江涛
     * @Date  2022/10/12 13:25
     */
    @RequiresPermissions("admin:admin:list")
    @RequestMapping("admin/list")
    public BaseRespVo list(BasePageInfo basePageInfo, String username) {

        CommonData<AdminListVO> commonDataVO = adminService.adminList(basePageInfo, username);
        return BaseRespVo.ok(commonDataVO);
    }

    /*
     * 添加管理员
     * http://182.92.235.201:8083/admin/admin/create
     *
     * @Author 12-江涛
     * @Date  2022/10/12 13:25
     */
    @RequiresPermissions("admin:admin:create")
    @RequestMapping("admin/create")
    public BaseRespVo create(@RequestBody MarketAdmin marketAdmin) {

        AdminCreateVO adminCreateVO = adminService.adminCreate(marketAdmin);

        if (adminCreateVO == null) {
            return BaseRespVo.invalidParameter("用户名重复");
        }
        return BaseRespVo.ok(adminCreateVO);
    }

    /*
     * 修改管理员信息
     * http://182.92.235.201:8083/admin/admin/update
     *
     * @Author 12-江涛
     * @Date  2022/10/12 13:25
     */
    @RequiresPermissions("admin:admin:update")
    @RequestMapping("admin/update")
    public BaseRespVo update(@RequestBody MarketAdmin marketAdmin) {

        AdminUpdateVO adminUpdateVO = adminService.adminUpdate(marketAdmin);
        if (adminUpdateVO == null) {
            return BaseRespVo.invalidParameter("操作失败");
        }

        return BaseRespVo.ok(adminUpdateVO);
    }


    /*
     * 删除管理员
     * http://182.92.235.201:8083/admin/admin/delete
     *
     * @Author 12-江涛
     * @Date  2022/10/12 13:25
     */
    @RequiresPermissions("admin:admin:delete")
    @RequestMapping("admin/delete")
    public BaseRespVo delete(@RequestBody MarketAdmin marketAdmin) {

        int count = adminService.adminDelete(marketAdmin);
        if (count == 1) {
            return BaseRespVo.ok();
        }

        return BaseRespVo.invalidParameter("操作失败！");

    }

}
